<template>
  <div class="admin-layout">
    <el-container>
      <el-aside width="200px">
        <div class="logo">管理员系统</div>
        <el-menu
          :default-active="activeIndex"
          class="el-menu-vertical"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
          :router="true">
          <el-menu-item index="/admin/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">首页</span>
          </el-menu-item>
          <el-menu-item index="/admin/users">
            <i class="el-icon-user"></i>
            <span slot="title">用户管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/order">
            <i class="el-icon-s-order"></i>
            <span slot="title">订单管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/services">
            <i class="el-icon-s-cooperation"></i>
            <span slot="title">服务类型管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/clothes">
            <i class="el-icon-s-goods"></i>
            <span slot="title">衣物类型管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/announcements">
            <i class="el-icon-bell"></i>
            <span slot="title">公告管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/news">
            <i class="el-icon-document"></i>
            <span slot="title">新闻管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <div class="header-right">
            <span>{{ username }}</span>
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link">
                <i class="el-icon-setting"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>
        <el-main>
          <slot></slot>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'AdminLayout',
  data() {
    return {
      activeIndex: this.$route.path
    }
  },
  computed: {
    username() {
      const user = JSON.parse(localStorage.getItem('user') || '{}')
      return user.username || '未登录'
    }
  },
  watch: {
    $route(to) {
      this.activeIndex = to.path
    }
  },
  methods: {
    handleCommand(command) {
      if (command === 'logout') {
        this.logout()
      }
    },
    logout() {
      this.$confirm('确认退出登录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem('user')
        this.$message.success('已退出登录')
        this.$router.push('/login')
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}
.el-aside {
  background-color: #304156;
  color: #fff;
  height: 100vh;
}
.logo {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  color: #fff;
  background-color: #263445;
}
.el-menu {
  border-right: none;
}
.el-header {
  background-color: #fff;
  color: #333;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}
.header-right {
  display: flex;
  align-items: center;
}
.header-right span {
  margin-right: 15px;
}
.el-dropdown-link {
  cursor: pointer;
}
.el-main {
  background-color: #f0f2f5;
  padding: 0;
}
</style> 